<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
  <style>
    .todos{
      border: 1px solid #333;
      border-radius: 6px;
      padding: 20px;
      width: 600px;
      margin: 20px auto;
    }
    .todo-content{
      margin-top: 20px;
    }
  </style>
</head>
<body>
  <div id="app">
      <div class="todos">
        <div class="todo-header row">
          <div class="col-md-10">
            <input type="text" v-model.trim="inputTxt" class="form-control">
          </div>
          <div class="col-md-2">
            <button class="btn btn-primary" @click="addTodo">增加</button>
          </div>
        </div>

        <div class="todo-content">
          <ul class="list-group">
            <li v-if="todos.length === 0" class="list-group-item text-center">
                暂无待办事项
            </li>
            <li
              v-for="(todo, index) in todos"
              class="list-group-item">
              <span>
                {{todo.content}}
              </span>
              <div class="pull-right">
                <button :disabled="todo.finished" @click="setTdodoFinished(index)" class="btn btn-xs btn-success">{{todo.finished?'已完成':'点击完成'}}</button>
                <button class="btn btn-xs btn-danger" @click="delTodo(index)">删除</button>
              </div>
            </li>
          </ul>
        </div>
      </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        inputTxt: '',
        todos: [
        ]
      },
      methods: {
        addTodo(){
          if(this.inputTxt === '') {
            return false;
          }
          // 增加待办事项
          this.todos.push({
            content: this.inputTxt,
            finished: false
          })
          this.inputTxt = '';
        },
        delTodo(index){
          // 删除待办事项
          if(confirm('您确认删除吗?')){
            this.todos.splice(index, 1);
          }
        },
        setTdodoFinished(index){
          // 设置待办事项已完成
          this.todos[index].finished = true;
        }
      }
    })
  </script>
</body>
</html>